angular.module('masvida.services', [])
.constant('MasVidaApiUrl', 'http://masvidafellowshipone.azurewebsites.net/api')
// .constant('MasVidaApiUrl', 'http://localhost:8100/api')
.service('PushNotificationService', function ($state, $http, MasVidaApiUrl) {

  return {
    register: function (userLogged) {
      var io = Ionic.io();
      var push = new Ionic.Push({
        // Removed for now
        // "onNotification": function(notification) {
        //    $state.go('tab.post-attendance',
        //     { attendanceSummaryId: notification._payload.attendanceSummaryId,
        //       date: notification._payload.date,
        //       met: notification._payload.met,
        //       isPosted: notification._payload.isPosted,
        //       comments: notification._payload.comments
        //    });
        // },
        "pluginConfig": {
          "android": {
            "iconColor": "#000000"
          }
        }
      });
      var user = Ionic.User.current();

      if (!user.id) {
        user.id = Ionic.User.anonymousId();
      }

      user.set('name', userLogged.Name);
      user.set('personId', userLogged.PersonId);
      user.save();

      var callback = function(data) {
        push.addTokenToUser(user);
        user.save();
      };
      push.register(callback);
      var request = userLogged;
      request.Token = user.id;
      var url = MasVidaApiUrl + "/PushNotifications/RegisterToken";
      $http.post(url, angular.toJson(request));
    }
  };
})
.service('LoginService', function ($http, MasVidaApiUrl) {
    return {
        loginUser: function (username, password, loginType) {
            var url = MasVidaApiUrl + '/Login/Login';

            var user = {
                Username: username,
                Password: password,
                LoginType: loginType,
                Ticket: null,
                ApiKey: null
            }

            return $http
                .post(url, angular.toJson(user));
        },
        isUserLogged: function () {
            return window.localStorage['user'] != undefined && window.localStorage['user'] != null && window.localStorage['user'] != "null";
        },
        logout: function () {
            window.localStorage['user'] = null;
        },
        saveUser: function (user) {
            window.localStorage['user'] = JSON.stringify(user);
        },
        getUserLogged: function () {
            return JSON.parse(window.localStorage['user'] || '{}');
        }
    }
})

.service('AttendanceService', function ($http, MasVidaApiUrl) {
    return {
        getGroups: function (request) {
            var url = MasVidaApiUrl + "/Group/GetGroupsForUser";
            return $http
                .post(url, angular.toJson(request));
        },
        getAttendances: function(request) {
            var url = MasVidaApiUrl + "/AttendanceSummary/GetAttendanceSummaryByGroup";
            return $http
                .post(url, angular.toJson(request));
        },
        getAttendanceDetailForAttendanceId: function(request) {
            var url = MasVidaApiUrl + "/Attendance/GetAttendancesByAttendanceSummaryId";
            return $http
                .post(url, angular.toJson(request));
        },
        saveAttendance: function(request) {
            var url = MasVidaApiUrl + "/AttendanceSummary/UpdateAttendanceSummary";
            return $http
                .post(url, angular.toJson(request));
        },
        saveAttendances: function(request) {
            var url = MasVidaApiUrl + "/Attendance/UpdateAttendances";
            return $http
                .post(url, angular.toJson(request));
        }
    };
});
